# 导入库
import pandas as pd 
import warnings # 忽略打印的警告
warnings.filterwarnings('ignore')

a_food = pd.read_csv('A网站美食数据.csv',encoding='gbk')
b_food = pd.read_csv('B网站美食数据.csv',encoding='gbk')
local = pd.read_csv('店铺地图信息.csv',encoding='gbk')
display(a_food.head())
display(b_food.head())
display(local.head())

# 数据清洗
# 合并数据集
# 修改 A 的【名称】列为【店铺名称】，【类别】列为【菜系】
# 【人均价格】的【元】去掉
a_food.rename(columns={'名称':'店铺名称','类别':'菜系'},inplace=True)
a_food['人均价格'] = a_food['人均价格'].str.replace('元','')
a_food.head(5)

# 纵向合并两个网站数据，去重
display(a_food.shape)
display(b_food.shape)
food = pd.concat([a_food,b_food],ignore_index=True).drop_duplicates()
display(food.shape)

# 以【店铺id】为键进行合并，没有 ID 的店铺会丢掉
food1 = pd.merge(food,local,on='店铺ID')
food1.shape

df = food1[['店铺名称','评论数','人均价格','菜系','商圈','推荐1','推荐2','推荐3',
           '口味评分','环境评分','服务评分','星级','纬度','经度','行政区名称']]
df.info()

# 数据可视化
from pyecharts.charts import Geo
from pyecharts import options as opts
from pyecharts.globals import GeoType
from pyecharts.globals import ThemeType

def geo():
    g = Geo(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))
    g.add_schema(maptype='广州')

    # 定义坐标对应的名称，添加到坐标库中 add_coordinate(name, lng, lat)
    names = list(df['店铺名称'])
    lng = list(df['经度'])
    lat = list(df['纬度'])
    for i in range(len(names)):
        g.add_coordinate(names[i],lng[i],lat[i])

    # 定义数据对，
    data_pair = [(name,50) for name in names]

    # 将数据添加到地图上
    g.add('', data_pair, type_=GeoType.EFFECT_SCATTER, symbol_size=3)

    # 设置样式
    g.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    g.set_global_opts(
            visualmap_opts=opts.VisualMapOpts(is_show=False),
            title_opts=opts.TitleOpts(title="广州美食分布"),
        )
    g.render('广州美食分布.html')
    return g
	
	# 绘制饼图
	from pyecharts import options as opts
	from pyecharts.charts import Pie
	from pyecharts.globals import ThemeType
	
	def pie():
	    x = ['天河区', '越秀区', '海珠区', '荔湾区', '番禺区', '白云区', '黄埔区']
	    y = [164, 69, 68, 27, 23, 20, 1]
	
	    c = (
	        Pie(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))
	        .add("", [list(z) for z in zip(x, y)])
	        .set_global_opts(title_opts=opts.TitleOpts(title="行政区美食占比"))
	        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
	    )
	    c.render('行政区美食占比.html')
	    return c

# 商圈分析
bussiness_area = df['商圈'].value_counts()
bussiness_area

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType

def bar():
    x = ['珠江新城','天河城/体育中心','北京路','江南西','天河北','机场路',
         '江南大道','高德置地/花城汇','石牌/龙口','兴盛路/跑马场']
    y = [60, 42, 34, 23, 22, 10, 10, 10, 10, 10]

    c = (
        Bar(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))
        .add_xaxis(x)
        .add_yaxis("",y)
        .set_global_opts(
            title_opts=opts.TitleOpts(title="商圈分布"),
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)))
    )
    c.render('商圈分布.html')
    return c
	
	# 星级分析
	start = df['星级'].value_counts()
	start
	
	from pyecharts import options as opts
	from pyecharts.charts import Pie
	from pyecharts.globals import ThemeType
	
	def pie_circle():
	    x = ['四星商户','准五星商户','五星商户','准四星商户']
	    y = [175,156,34,7]
	
	    c = (
	        Pie(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))
	        .add(
	            "",
	            [list(z) for z in zip(x,y)],
	            radius=["40%", "55%"],
	            label_opts=opts.LabelOpts(
	                position="outside",
	                formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c}  {per|{d}%}  ",
	                background_color="#eee",
	                border_color="#aaa",
	                border_width=1,
	                border_radius=4,
	                rich={
	                    "a": {"color": "#999", "lineHeight": 22, "align": "center"},
	                    "abg": {
	                        "backgroundColor": "#e3e3e3",
	                        "width": "100%",
	                        "align": "right",
	                        "height": 22,
	                        "borderRadius": [4, 4, 0, 0],
	                    },
	                    "hr": {
	                        "borderColor": "#aaa",
	                        "width": "100%",
	                        "borderWidth": 0.5,
	                        "height": 0,
	                    },
	                    "b": {"fontSize": 16, "lineHeight": 33},
	                    "per": {
	                        "color": "#eee",
	                        "backgroundColor": "#334455",
	                        "padding": [2, 4],
	                        "borderRadius": 2,
	                    },
	                },
	            ),
	        )
	        .set_global_opts(title_opts=opts.TitleOpts(title="星级占比"))
	    )
	    c.render('星级占比.html')
	    return c